package com.lg.happyfarmer.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;

import com.lg.happyfarmer.R;

import xtom.frame.XtomAdapter;
import xtom.frame.XtomFragment;
import xtom.frame.net.XtomNetWorker;

public abstract class BaseAdapter extends XtomAdapter {
    protected static final int VIEWTYPE_EMPTY = 0;
    protected static final int VIEWTYPE_NORMAL = 1;

    private String emptyString = "列表为空";
    private TextView emptyTextView;
    private int failtype;

    public BaseAdapter(Context mContext) {
        super(mContext);
    }

    public BaseAdapter(XtomFragment mFragment) {
        super(mFragment);
    }

    @Override
    public int getItemViewType(int position) {
        if (isEmpty())
            return VIEWTYPE_EMPTY;
        return VIEWTYPE_NORMAL;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    /**
     * 获取列表为空时的显示View(调用此方法(不重写getItemViewType时)需重写isEmpty()方法)
     *
     * @return a view 传递getView方法中的ViewGroup参数即可
     */
    public View getEmptyView(ViewGroup parent) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.listitem_empty_base, null);
        emptyTextView = (TextView) view.findViewById(R.id.textview);
        ViewGroup.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth(), (int) (parent.getHeight()));
        view.setLayoutParams(params);
        if (failtype == XtomNetWorker.FAILED_NONETWORK) {
            Drawable drawable = mContext.getResources().getDrawable(
                    R.mipmap.img_nonetwork);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            emptyTextView.setCompoundDrawablePadding(BaseUtil.dip2px(mContext, 20));
            emptyTextView.setCompoundDrawables(null, drawable, null, null);
            emptyTextView.setText("网络不给力撒，请到有网的条件下浏览");
        } else {
            emptyTextView.setText(emptyString);
            Drawable cryDrawable = mContext.getResources().getDrawable(R.mipmap.cry_face);
            cryDrawable.setBounds(0, 0, cryDrawable.getMinimumWidth(),
                    cryDrawable.getMinimumHeight());
            emptyTextView.setCompoundDrawablePadding(BaseUtil.dip2px(mContext, 20));
            emptyTextView.setCompoundDrawables(null, cryDrawable, null, null);
        }
        return view;
    }

    /**
     * 设置空列表提示语
     *
     * @param emptyString
     */
    public void setEmptyString(String emptyString) {
        if (emptyTextView != null)
            emptyTextView.setText(emptyString);
        this.emptyString = emptyString;
    }

    /**
     * 设置空列表图标
     *
     * @param
     */
    public void setEmptyDraw(Drawable drawable) {
        if (emptyTextView != null)
            emptyTextView.setCompoundDrawables(null, drawable, null, null);
    }

    /**
     * 设置空列表提示语
     *
     * @param emptyStrID
     */
    public void setEmptyString(int emptyStrID) {
        emptyString = mContext.getResources().getString(emptyStrID);
        setEmptyString(emptyString);
    }

    public void setFailtype(int failtype) {
        this.failtype = failtype;
        if (emptyTextView != null) {
            if (failtype == XtomNetWorker.FAILED_NONETWORK) {
                Drawable drawable = mContext.getResources().getDrawable(
                        R.mipmap.img_nonetwork);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                        drawable.getMinimumHeight());
                emptyTextView.setCompoundDrawablePadding(BaseUtil.dip2px(mContext, 20));
                emptyTextView.setCompoundDrawables(null, drawable, null, null);
                emptyTextView.setText("网络不给力撒，请到有网的条件下浏览");
            } else {
                emptyTextView.setText(emptyString);
                Drawable cryDrawable = mContext.getResources().getDrawable(R.mipmap.cry_face);
                emptyTextView.setCompoundDrawables(null, cryDrawable, null, null);
            }
        }
    }

}
